﻿
var webFlag = 0;
var webIndex = 0;


var urlBeans;

function getUrls() {
	$.post("userUrl", {
		"types" : "getUrls"
	}, function(data) {

		if (data != null) {
			urlBeans = data;
			setUrlTableData();
			loadTable(0);
		}
	}, "json");
}
function getWebTestNumber(){
	
	$.post("userUrl", {"types":"getWebTestNumber"},
			   function(data){
		webTestCount = data;
			   },"json");
	
}
function setUrlTableData() {
	var urls = "";
	for ( var i = 0; i < urlBeans.length; i++) {
		var bean = urlBeans[i];
		urls = urls + " <li>" + bean.testUrl + "</li>";
	}
	$("#ulUrlList").html(urls);
}

function loadTable(type) {
	var tableStr = "";
	tableStr = tableStr
			+ "<thead><tr><th>测试网址</th><th style='display:none;'>拨测次数</th><th>及时响应时间</th><th>测试进度</th><th>平均时间</th><th>测试抖动</th><th>丢包率</th></tr></thead><tbody>";

	for ( var i = 0; i < urlBeans.length; i++) {
		var bean = urlBeans[i];
		tableStr = tableStr
				+ "<tr><td  >"
				+ bean.testUrl
				+ "</td><td style='display:none;'>&nbsp</td><td></td><td></td><td ></td><td ></td><td ></td></tr>";
}

	tableStr = tableStr + "</tbody>";

	
	if(type==0){
		
		$("#allTestWebTable").html(tableStr);
		$("#webtable").html(tableStr);
	}else if(type==1){
		
		$("#allTestWebTable").html(tableStr);
	}else if(type==2){
		$("#webtable").html(tableStr);
		
	}
	
	
}

function webTestBtnClick() {
	
	
	if(!webTestCheak()){
		
		return;
	}
	
	
	webtableName="webtable";
	if (webFlag == 0) {
		webFlag = 1;
		$("#btnWeb").html("测速中...");
		$("#btnWeb").attr("disabled", true);

		//  $("#webtable").show();

		//清空表格
		loadTable(2);
		runTestWeb();

	} else if (webFlag == 1) {
		webFlag = 0;
		$("#btnWeb").attr("disabled", false);
		$("#btnWeb").html("重新测速");
	}


}

var intStartTime;
var objIMG = new Image();
var intTimerID;

var runI = 0;

var runJ = 1;

var webtableName;


var webTestCount = 2;


var webTestDataArry = new Array();
function runTestWeb() {
		


	
	if(runJ==(webTestCount+1)){
			runI++;
			runJ=1;
		}
	
	
	
	if (runI <(urlBeans.length)) {

			webTest(runI);
	
	} else {
		

		runI=0;
		if(webtableName=="webtable"){
			
			webTestBtnClick();
			
		}else if(webtableName=="allTestWebTable"){
			
			allTestDownFile();
		}
	
		setWebData();
		
	}

}


var maxSpeed;

var mainSpeed;
var speedCount;
var testCount=1;

var webTestState = true;

function webTest(i) {
	
	runJ++;
	
	webTestState = true;
		
		var bean = urlBeans[i];

		//alert(bean.testUrl);

		intStartTime = +new Date();
		objIMG.src ="http://"+ bean.testUrl + "/" + intStartTime;
		intTimerID = setTimeout(timeout, 2000);
		
		var tr = $("#"+webtableName+" > tbody > tr:eq(" + i + ")");
	
	

		//tr.find("td:eq(1)").html(runJ-1);



		objIMG.onload = objIMG.onerror = function() {

			
			if(!webTestState){
				
				return;
			}
			

		clearTimeout(intTimerID);

		var delay = new Date() - intStartTime;

		if (delay < 500) {
				tr.find("td:eq(2)").html((delay / 1000) + " 秒");
				tr.find("td:eq(3)").html(getTestPlan(runJ-1));
				tr.find("td:eq(2)").css("color", "#0000CD");
		} else if (delay < 1000) {
		
				tr.find("td:eq(2)").html((delay / 1000) + " 秒");
				tr.find("td:eq(3)").html(getTestPlan(runJ-1));
				tr.find("td:eq(2)").css("color", "#8B4513");

		} else if (delay >= 999 && delay < 1999) {
		
				tr.find("td:eq(2)").html(">1秒");
				tr.find("td:eq(3)").html(getTestPlan(runJ-1));
				tr.find("td:eq(2)").css("color", "#FF6A6A");
			
		}
	
		
		if((runJ-1)==1){
			 testCount=1;
			maxSpeed = delay;
			speedCount = delay;
			mainSpeed = delay;
		}else if(runJ==(webTestCount+1)){
			testCount++;
			speedCount = speedCount+delay;
			
			if(delay>maxSpeed){
				maxSpeed=delay;
			}
			
			if(delay<mainSpeed){
				mainSpeed = delay;
			}
			
	
			calculateTest();
			
			
		}else{
			testCount++;
			speedCount = speedCount+delay;
			
			if(delay>maxSpeed){
				maxSpeed=delay;
			}
			
			if(delay<mainSpeed){
				mainSpeed = delay;
			}
			
	
			
		}
		
	
		setTimeout(runTestWeb, delay < 1000 ? (1000 - delay) : 1000);
	};
	
	

}





function calculateTest(){
	
	var tr = $("#"+webtableName+" > tbody > tr:eq(" + runI + ")");
	
	//丢包率
	var lostRate = (((webTestCount-testCount)/webTestCount)*100).toFixed(1);
	//抖动
	var shake = maxSpeed-mainSpeed;
	
	var timeDelayAvr = 0;
	
	if(testCount!=0){
		
		timeDelayAvr = (speedCount/testCount).toFixed(3);
	}
	
	tr.find("td:eq(4)").html((timeDelayAvr/1000) +" 秒");
	
	tr.find("td:eq(5)").html(((shake)/1000)+" 秒");
	tr.find("td:eq(6)").html(lostRate+"%");
	
	
	//将数据保存起来
	var obj = new Object();
	
	obj["lostRate"] = lostRate;
	obj["shake"] = shake;
	obj["timeDelayAvr"] = timeDelayAvr;
	obj["timeDelayMax"] = maxSpeed;
	obj["timeDelayMin"] = mainSpeed;
	obj["testCount"] = webTestCount;
	var bean = urlBeans[runI];
	obj["url"] = bean.testUrl;
	webTestDataArry.push(obj);
	
}

function timeout() {
	var j = runJ-1;
	var tr = $("#"+webtableName+" > tbody > tr:eq(" + runI + ")");

		tr.find("td:eq(2)").html("请求超时");
		tr.find("td:eq(3)").html(getTestPlan(j));
		tr.find("td:eq(2)").css("color", "#696969");
		
		
		
		if((runJ-1)==1){
			 testCount=0;
			 maxSpeed = 0;
			 speedCount = 0;
			 mainSpeed = 0;
		}else if(runJ==(webTestCount+1)){
			
			calculateTest();
		}

		webTestState = false;
		runTestWeb();
};


function getTestPlan(j){
		var str = "<div class='processbar3'>";	
			str	= str+"<div class='finish3' style='width: "+((j/webTestCount)*100)+"%;'></div>";
		str	= str+"</div>";
	return str;
}

function setWebData() {

	

	
	for(var i= 0; i < webTestDataArry.length;i++){
		var obj = webTestDataArry[i];
		
		
		$.post("setData", {
			"types" : "web",
			"nameNumber" : "testNumber","lostRate":	obj["lostRate"] ,"shake":obj["shake"],"timeDelayAvr":obj["timeDelayAvr"],"timeDelayMax":obj["timeDelayMax"],
			"timeDelayMin":	obj["timeDelayMin"],"testCount":obj["testCount"],"url":obj["url"]
		});
	
	}
	webTestDataArry.length = 0;
	webTestDataArry = new Array();


}
